//
//  AppBaseVC.swift
//  
//
//  Created by bo wang on 2021/3/3.
//  Copyright © 2021 wangbo. All rights reserved.
//

import UIKit
import IQKeyboardManager

class AppBaseVC: UIViewController {
    
    /// 设置这个值可以控制手势返回是否可用
    @IBInspectable var disablePopGesture: Bool = false
    /// 设置这个值可以控制系统导航栏是否可见。
    @IBInspectable var isNavigationBarHidden: Bool = false
    /// 设置这个值可以控制是否在当前控制器界面上启用IQKeyboardManager的点击退出编辑功能。默认关闭的。
    @IBInspectable var isTouchResignEnabled: Bool = false {
        didSet {
            updateIQTouchResign()
        }
    }
    
    deinit {
        Log.info("✅\(Self.self) deinit! ✅")
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        if parent is UINavigationController {
            navigationController?.setNavigationBarHidden(isNavigationBarHidden, animated: true)
        }
        updateIQTouchResign()
        Log.info("---- \(Self.self) WillAppear ----")
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if parent is UINavigationController {
            navigationController?.setNavigationBarHidden(isNavigationBarHidden, animated: true)
        }
    }
    
    private func updateIQTouchResign() {
        if isTouchResignEnabled {
            IQKeyboardManager.shared().enabledTouchResignedClasses.add(Self.self)
        } else if IQKeyboardManager.shared().enabledTouchResignedClasses.contains(Self.self) {
            IQKeyboardManager.shared().enabledTouchResignedClasses.remove(Self.self)
        }
    }
    
    override var prefersStatusBarHidden: Bool { false }
    override var preferredStatusBarStyle: UIStatusBarStyle { .default }
    override var preferredStatusBarUpdateAnimation: UIStatusBarAnimation { .none }
}

extension AppBaseVC {
    @IBAction func onTapNaviBackBtn(_ btn: UIButton) {
        if let navi = navigationController, navi.topViewController === self {
            navi.popViewController(animated: true)
        }
    }
    @IBAction func onTapDismissBtn(_ btn: UIButton) {
        if let vc = presentingViewController {
            vc.dismiss(animated: true)
        }
    }
}
